<?php


namespace app\admin\controller\goods;


use app\common\controller\Adminbase;
use think\Db;

class Goods extends Adminbase
{
    protected $searchFields = 'name';

    protected function initialize()
    {
        parent::initialize();
        $goods_categoty = Db::name('goods_categoty')
            ->where('is_del', '<>', 1)
            ->order('sort desc')
            ->select();
        $this->assign('goodsCategoty', $goods_categoty);
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = Db::name('goods')
                ->order(array('id' => 'desc'))
                ->where($where)
                ->where('is_del', '<>', 1)
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['goods_cate'] = Db::name('goods_categoty')
                    ->find($v['cate_id']);
            }
            $total = Db::name('goods')
                ->where($where)
                ->where('is_del', '<>', 1)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $_list);

            return json($result);

        }
        return $this->fetch();
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $where[] = ['cate_id', '=', $data['goods_categoty']];
            $where[] = ['name', '=', $data['name']];
            $get_good = Db::name('goods')->where($where)->find();
            if ($get_good) {
                $this->error('商品名称已存在');
            }
            $insert_data = array(
                'name' => $data['name'],
                'cate_id' => $data['goods_categoty'],
                'detail' => $data['content'],
                'create_time' => time(),
                'price' => $data['price'],
                'purchase_price' => $data['purchase_price'],
                'specs' => $data['specs'],
                'count' => $data['count'],
                'old_price' => $data['old_price'],
                'sort' => $data['sort'],
                'img' => $data['img'],
                'recommend' => 0, //'是否推荐 0 正常 1推荐 默认0',
                'is_del' => 0,    //'是否删除 1 是 0 不是',
                'base_buy' => $data['base_buy'],
                'base_purchase' => $data['base_purchase'],
                'write_price' => $data['write_price']

            );
            $stmt = Db::name('goods')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }

        } else {
            return $this->fetch();
        }
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('goods')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 推荐
     */
    public function recommend()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('goods')->where('id', $id)->update(['recommend' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 是否商家显示
     */
    public function isBusiness()
    {
        $id = $this->request->param('id/d', 0);

        $isbi = $this->request->param('value/d');

        if (Db::name('goods')->where('id', $id)->update(['isb' => $isbi])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }
    /**
     * 开通水票
     */
    public function ticket()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('goods')->where('id', $id)->update(['is_ticket' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('goods')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
//        \Form::select();
        $this->success('删除成功！');
    }

    public function edit_img()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $old_url = Db::name('goods_imgs')->where('goods_id', $data['id'])->select();
            foreach ($old_url as $k => $v) {
                $del_url = parse_url($v['img']);
//                if (file_exists($del_url['path'])) {
//                    unlink($del_url);
//                }
            }
            $stmt = Db::name('goods_imgs')->where('goods_id', $data['id'])->delete();
            $img = explode(',', $data['img']);
            foreach ($img as $k => $v) {

                $insert_data = array(
                    'img' => $v,
                    'sort' => $k + 1,
                    'goods_id' => $data['id'],
                );
                $insert_data = Db::name('goods_imgs')->insertGetId($insert_data);
            }
            $this->success('增加成功');
            if ($insert_data) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
//            } catch (\Exception $e) {
//                Db::rollback();
//                $this->error($e->getMessage());
//            }
        } else {
            $id = $this->request->param('id/d', 0);

            $data = Db::name('goods_imgs')->where('goods_id', $id)->select();
            $imgs = '';
            foreach ($data as $k => $v) {
                $imgs .= $v['img'] . ',';
            }
            $data2['id'] = $id;
            $data2['img'] = $imgs;

            $this->assign('data', $data2);
            return $this->fetch('goods/goods/editimgs');
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'id' => $data['id'],
                'name' => $data['name'],
                'cate_id' => $data['goods_categoty'],
                'detail' => $data['content'],
                'price' => $data['price'],
                'purchase_price' => $data['purchase_price'],
                'specs' => $data['specs'],
                'count' => $data['count'],
                'old_price' => $data['old_price'],
                'img' => $data['img'],
                'base_buy' => $data['base_buy'],
                'base_purchase' => $data['base_purchase'],
                'sort' =>$data['sort'],
                'is_water' => $data['is_water'],
                'is_kong' => $data['is_kong'],
            );
            $stmt = Db::name('goods')->update($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $id = $this->request->param('id/d', 0);

            $data = Db::name('goods')->find($id);

            $this->assign('data', $data);
            return $this->fetch('goods/goods/edit');
        }
    }
}